import PostCategories from "../models/PostCategories"
import Post from "../models/Post";

const createPostCategory = async (req, res, next) => {
    try {
        const {
            title
        } = req.body

        const postCategory = await PostCategories.findOne({
            title
        });

        if (postCategory) {
            const error = new Error("类型已创建");
            return next(error)
        }

        const newPostCategory = new PostCategories({
            title
        })

        const savedPostCategory = await newPostCategory.save()

        return res.status(201).json(savedPostCategory)
    } catch (error) {
        next(error)
    }
}

const getSingleCategory = async (req, res, next) => {
    try {
        const postCategory = await PostCategories.findById(
            req.params.postCategoryId
        );

        if (!postCategory) {
            const error = new Error("未找到类别!");
            return next(error);
        }

        return res.json(postCategory);
    } catch (error) {
        next(error);
    }
};

const getAllPostCategories = async (req, res, next) => {
    try {
        const filter = req.query.searchKeyword;
        let where = {}
        if (filter) {
            where.title = {
                $regex: filter,
                $options: "i"
            };
        }
        let query = PostCategories.find(where);
        const page = parseInt(req.query.page) || 1;
        const pageSize = parseInt(req.query.limit) || 10;
        const skip = (page - 1) * pageSize;
        const total = await PostCategories.find(where).countDocuments();
        const pages = Math.ceil(total / pageSize);

        res.header({
            'x-totalcount': JSON.stringify(total),
            'x-currentpage': JSON.stringify(page),
            'x-pagesize': JSON.stringify(pageSize),
            'x-totalpagecount': JSON.stringify(pages),
        })

        if (page > pages) {
            return res.json([])
        }

        const result = await query
            .skip(skip)
            .limit(pageSize)
            .sort({
                updatedAt: 'desc'
            });
        return res.json(result)
    } catch (error) {
        next(error)
    }
}

const updatePostCategory = async (req, res, next) => {
    try {
        const {
            title
        } = req.body

        const postCategory = await PostCategories.findByIdAndUpdate(
            req.params.postCategoryId, {
                title,
            }, {
                new: true, // 返回更新后的数据
            }
        );
        if (!postCategory) {
            const error = new Error("类型不存在");
            return next(error)
        }

        return res.json(postCategory)
    } catch (error) {
        next(error)
    }
}

const deletePostCategory = async (req, res, next) => {
    try {
        const categoryId = req.params.postCategoryId;

        await Post.updateMany({
            categories: {
                $in: [categoryId]
            },
        }, {
            $pull: {
                categories: categoryId
            }
        }, )

        await PostCategories.deleteOne({
            _id: categoryId
        });

        res.send({
            message: "文章类型删除成功"
        })
    } catch (error) {
        next(error)
    }
}

export {
    createPostCategory,
    getAllPostCategories,
    updatePostCategory,
    deletePostCategory,
    getSingleCategory,
}